<?php namespace Shop\Controller;
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------


use Admin\Api\SecurityCodeApi;
use Admin\Model\SecurityCodeModel;
use Common\Api\AccountApi;
use Common\Api\BoyeServiceApi;
use Shop\Api\BannersApi;
use Shop\Api\MemberConfigApi;
use Shop\Model\ProductModel;
use Uclient\Model\OAuth2TypeModel;
use Shop\Api\ProductApi;
use Shop\Api\ProductGroupApi;
use Uclient\Model\UcenterMemberModel;

/**
 * 商城核心控制器
 *
 * Class IndexController
 * @package Shop\Controller
 * @author hebiduhebi@126.com
 */
class IndexController extends ShopController{

    /**
     * 初始化
     */
    public function _initialize(){
        parent::_initialize();
    }

	/*
	 *
	 * 首页
	 * @author 老胖子-何必都 <hebiduhebi@126.com>
	 * */
    public function index(){

        $position = getDatatree('SHOP_INDEX_BANNERS');

        $map = array(
            'position'=>$position,
        );

        $result=apiCall(BannersApi::QUERY_WITH_POSITION,array($map));
        $this->checkApiCallResult($result);

        $banners = $result['info']['list'];
        $this->assign('banners',$banners);
        //**********1. BANNERS****************

//		$map=array('onshelf'=>1,'status'=>1);
//		$order = " createtime desc ";
//		$page = array('curpage'=>0,'size'=>4);
//		$result_new = apiCall(ProductApi::QUERY,array($map,$page,$order));
//
//		$this->assign('new',$result_new['info']['list']);
        //**********2. 最新商品*****************

		$map= array(
			'g_id'=>getDatatree('FLASH_SALE'),
			'start_time'=>array(
				'LT',time()
			),
		);

		$result = apiCall(ProductGroupApi::QUERY_WITH_PRODUCT,array($map));
		if(!$result['status']){
			$this->error($result['info']);
		}
//        dump($result['info']);
        $this->assign('flash_sales',$result['info']);
        //**********3. 限时抢购 *****************

		$map= array(
			'g_id'=>getDatatree('HOT'),
		);

		$result = apiCall(ProductGroupApi::QUERY_WITH_PRODUCT,array($map));

		$this->assign('hot',$result['info']);
		$this->theme($this->themeType)->display();
    }

	/*
	 * 商品分类
	 * @author 老胖子-何必都 <hebiduhebi@126.com>
	 * */
	public function lists(){

        $country =  I('get.country','-1');
        $cate_id = I('get.id','-1');

        $map=array('onshelf'=>ProductModel::STATUS_ONSHELF,'status'=>1);

        if($country != "-1"){
            $map['dt_origin_country']= $country;
        }else{
            $map['cate_id']= $cate_id;
        }
        $page = array('curpage'=>I('p',1));
		$result=apiCall(ProductApi::QUERY,array($map,$page));


        $this->checkApiCallResult($result);



		$this->assign('lists',$result['info']['list']);
		$this->theme($this->themeType)->display();
	}

	/*
	 * 搜索
	 * @author 老胖子-何必都 <hebiduhebi@126.com>
	 * */
	public function search(){

		$s_name = I('s_name','');

		$map['name'] = array('like','%'. $s_name . '%');
        $map['status'] = 1;
        $map['onshelf'] = ProductModel::STATUS_ONSHELF;

		$result=apiCall(ProductApi::QUERY_NO_PAGING,array($map));

		$this->assign('lists',$result['info']);
        $this->assign("s_name",$s_name);
		$this->theme($this->themeType)->display('Index/lists');
	}
	
	/**
	 *
	 * 注册
	 * @author hebidu <hebiduhebi@126.com>
	 * @date  15/11/23 08:49
	 * @copyright by itboye.com
	 */
	public function register(){

		if(IS_GET){
            $invite_id = I('get.invite_id','');
            $this->assign("invite_id",$invite_id);
			$this->theme($this->themeType)->display();
		}else{

            $idcode =  $this->_post('idcode','');
            $invite_id = 0;

            if(!empty($idcode)){
                $result = apiCall(MemberConfigApi::GET_INFO,array(array('idcode'=>$idcode)));
                if($result['status'] && is_array($result['info'])){
                    $invite_id = $result['info']['uid'];
                }
            }

            $mobile =  $this->_post('mobile','','手机号缺少!');

            $pwd = $this->_post("pwd",'',"密码必须");
            $pwd = trim($pwd);
            $pwd_repeat = $this->_post("pwd1",'',"密码必须");

            //1. 密码是否一致
            if($pwd != $pwd_repeat){
                $this->error("2次密码不一致!");
            }

            $code = $this->_post('code','','验证码必填!');
            //2. 验证码验证
            $this->checkCode($mobile,$code,SecurityCodeModel::TYPE_FOR_REGISTER);

            $data = array(
                'username'=>$mobile,
                'from'=>OAuth2TypeModel::SELF,
                'type'=> UcenterMemberModel::ACCOUNT_TYPE_MOBILE,
                'password'=>$pwd,
                'invite_id'=>$invite_id,//邀请人的idcode
            );

            $result = $this->remoteApi->callRemote("User/register",$data);

			if($result['status']){
                $apiResult = $result['info'];
                if(is_array($apiResult) && $apiResult['code'] == 0){
                    $result = apiCall(MemberConfigApi::GET_INFO,array(array('uid'=>$apiResult['data'])));
                    if($result['status'] && is_array($result['info'])){
                        $this->success('注册成功!',U('Shop/Index/succshow',array('idcode'=>$result['info']['idcode'])));
                    }else{
                        $this->error("注册失败!");
                    }
                }else{
                    $this->error($apiResult['data']);
                }
			}else{
				$this->error($result['info']);
			}
		}
	}

	/**
	 * 注册成功跳转
	 */	
	public function succshow(){

        $idcode = $this->_get("idcode","");

		$this->assign('idcode',$idcode);
		$this->theme($this->themeType)->display();
	}

	/*
	 * 登录
	 * */
	public function login(){
		if(IS_GET){
			$this->theme($this->themeType)->display();
		}else{
			$username=I('uname','');
			$password=I('pwd','');
			$type=1;
			$from=OAuth2TypeModel::SELF;
			$result=apiCall(AccountApi::LOGIN,array($username,$password,$type,$from));
			if($result['status']){
				$id=$result['info'];
				$user=apiCall(AccountApi::GET_INFO,array($id));
                $user['head']= getImageUrl($user['head']);
				session('user',$user['info']);
				$this->success('登录成功!正在跳转登录页面',U('Shop/Index/index'));
			}else{
				$username=I('uname','');
				$password=I('pwd','');
				$type=3;
				$from=OAuth2TypeModel::SELF;

				$result=apiCall(AccountApi::LOGIN,array($username,$password,$type,$from));
				if($result['status']){
					$id=$result['info'];
					$user=apiCall(AccountApi::GET_INFO,array($id));
					session('user',$user['info']);

					$this->success('登录成功!正在跳转',U('Shop/Index/index'));
				}else{
					$this->error($result['info']);
				}
				
			}
		}
	}

	/**
	 * 手机app
	 */
	public function phoneapp(){
		$this->theme($this->themeType)->display();
	}

	/*
	 * 退出登录
	 * @AUTHOR hebiduhebi@126.COM
	 * */
	public function loginout(){
        $this->clearCartItemCache();

        session(null);
        session("[destroy]");

        $this -> redirect('Shop/Index/index');
	}

	/*
	 * 用户协议
	 * */
	public function xieyi(){
		$this->theme($this->themeType)->display();
	}

    /**
     * 获取短信验证码
     * 参数mobile: 手机
     * @author 老胖子-何必都 <hebiduhebi@126.com>
     * @date  15/11/21 16:47
     * @copyright by itboye.com
     */
    public function getCode(){
        $service = new BoyeServiceApi();

        $mobile =  $this->_post('mobile','','缺少参数mobile');
        $type = SecurityCodeModel::TYPE_FOR_REGISTER;
        $data = array(
            'mobile'=>$mobile,
            'type'=>$type,
        );

        $result =$service->callRemote("Message/send",$data);
        if($result['status']){
            $info = $result['info'];
            if($info['code'] != 0){
                $this->error($info['data']);
            }else{
                $this->success($info['data']);
            }
        }else{
            $this->error($result['info']);
        }
    }

    private function checkCode($mobile,$code,$type){

        $result = apiCall(SecurityCodeApi::IS_LEGAL_CODE, array($code,$mobile,$type));

        if(!$result['status']){
            $this->error($result['info']);
            return false;
        }


    }
	
}

